package net.zubara.esni.core.client;

import java.io.NotSerializableException;
import java.util.Map;

import net.zubara.esni.core.json.JSONArray;
import net.zubara.esni.core.json.JSONObject;

import org.eclipse.ecf.remoteservice.IRemoteCall;
import org.eclipse.ecf.remoteservice.client.IRemoteCallable;
import org.eclipse.ecf.remoteservice.client.IRemoteResponseDeserializer;

public class JSONRemoteResponseDeserializer implements IRemoteResponseDeserializer {

	@Override
	public Object deserializeResponse(String endpoint, IRemoteCall call, IRemoteCallable callable, Map responseHeaders, String responseBody)
			throws NotSerializableException {
		try {
			Object jsonResponse = null;
			if (responseBody.startsWith("{")) {
				jsonResponse = new JSONObject(responseBody);
				return jsonResponse;
			} else if (responseBody.startsWith("[")) {
				jsonResponse = new JSONArray(responseBody);
			}
			return jsonResponse;
		} catch (Exception e) {
			throw new NotSerializableException("JSON parse exception: " + e.getMessage());
		}
	}

}
